/*
 * MIT License
 *
 * Copyright (c) 2023 芊芊墨客
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package com.zhangmeng.tools.views;

import com.sun.javafx.PlatformUtil;
import com.zhangmeng.tools.utils.ImagePath;
import com.zhangmeng.tools.utils.ResourcesUtils;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-02-16 09:19
 */
public class HomeView extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane fx =  FXMLLoader.load(ResourcesUtils.getResource("home"));
        Scene scene = new Scene(fx);
        Image image = new Image(ImagePath.path(ImagePath.ImagePathType.Tools_ICON));
        primaryStage.getIcons().add(image);
        primaryStage.setScene(scene);
        primaryStage.setTitle("java-fx 工具");
        primaryStage.show();

        primaryStage.setOnCloseRequest(event -> {

            if (PlatformUtil.isLinux()){
                primaryStage.setIconified(true);
                event.consume();
                return;
            }

            Platform.setImplicitExit(true);
            try {
                SystemTray systemTray = SystemTray.getSystemTray();
                java.awt.Image image1 = Toolkit.getDefaultToolkit().getImage(this.getClass().getClassLoader().getResource(ImagePath.path(ImagePath.ImagePathType.TRAY_ICON)));
                String title = "java-fx 系统托盘";
                PopupMenu menu = new PopupMenu();
                MenuItem item1 = new MenuItem("显示");
                MenuItem item2 = new MenuItem("退出");
                menu.add(item1);
                menu.add(item2);
                TrayIcon trayIcon = new TrayIcon(image1, title, menu);
                systemTray.add(trayIcon);
                item1.addActionListener(e -> {
                    Platform.runLater(primaryStage::show);
                    systemTray.remove(trayIcon);
                });

                item2.addActionListener(e -> {
                    Platform.setImplicitExit(true);
                    Platform.runLater(primaryStage::close);
                    systemTray.remove(trayIcon);
                    Platform.exit();
                });

                menu.addActionListener(e -> {
                    Platform.runLater(primaryStage::show);
                    systemTray.remove(trayIcon);
                });

                Platform.setImplicitExit(false);
                primaryStage.hide();
            } catch (AWTException e) {
                e.printStackTrace();
            }
        });
    }
}
